package com.samphanie.auiu.mbg.utils;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.samphanie.auiu.common.constants.AuiuConstants;
import com.samphanie.auiu.common.filter.SQLFilter;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 分页工具类
 *
 * @author ZSY
 * @email 1451691457@qq.com
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PageUtils {

    /**
     * 总记录数
     */
    private int totalCount;
    /**
     * 每页记录数
     */
    private int pageSize;
    /**
     * 总页数
     */
    private int totalPage;
    /**
     * 当前页数
     */
    private int currPage;
    /**
     * 列表数据
     */
    private List<?> list;
    /**
     * 指定日期成交额
     */
    private String turnover;

    public static void startPage(Integer pageNum, Integer pageSize) {
        // 开启分页
        startPage(pageNum, pageSize, null, null);
    }

    public static void startPage(Integer pageNum, Integer pageSize, String order, String orderByColumn) {

        //分页参数
        int curPage = 1;
        int limit = 10;

        if (pageNum != null) {
            curPage = pageNum;
        }

        if (pageSize != null) {
            limit = pageSize;
        }

        //排序字段
        //防止SQL注入（因为排序字段是通过拼接SQL实现排序的，会有SQL注入风险）
        String orderBy = SQLFilter.sqlInject(orderByColumn);
        if (StrUtil.isNotBlank(order)) {
            if (AuiuConstants.ASC.equalsIgnoreCase(order) || AuiuConstants.DESC.equalsIgnoreCase(order)) {
                orderBy = orderBy + " " + order;
            }
        }
        PageHelper.startPage(curPage, limit, orderBy);
    }

    public static void startPage(Map<String, Object> params) {
        //分页参数
        int pageNum = 1;
        int pageSize = 10;

        if (params.get(AuiuConstants.PAGE) != null) {
            pageNum = Integer.parseInt((String) params.get(AuiuConstants.PAGE));
        }
        if (params.get(AuiuConstants.LIMIT) != null) {
            pageSize = Integer.parseInt((String) params.get(AuiuConstants.LIMIT));
        }
        String order = (String) params.get("order");
        String orderByColumn = (String) params.get("column");
        if (StrUtil.isNotBlank(orderByColumn)) {
            String orderBy = SQLFilter.sqlInject(orderByColumn);
            if (StrUtil.isNotBlank(order)) {
                if (AuiuConstants.ASC.equalsIgnoreCase(order) || AuiuConstants.DESC.equalsIgnoreCase(order)) {
                    orderBy = orderBy + " " + order;
                }
            }
            // 开启分页
            PageHelper.startPage(pageNum, pageSize, orderBy);
        } else {
            // 开启分页
            PageHelper.startPage(pageNum, pageSize);
        }
    }


    /**
     * 分页
     *
     * @param list       列表数据
     * @param totalCount 总记录数
     * @param pageSize   每页记录数
     * @param currPage   当前页数
     */
    public PageUtils(List<?> list, int totalCount, int pageSize, int currPage) {
        this.list = list;
        this.totalCount = totalCount;
        this.pageSize = pageSize;
        this.currPage = currPage;
        this.totalPage = (int) Math.ceil((double) totalCount / pageSize);
    }

    public PageUtils(List<?> list, Integer currPage, Integer pageSize) {

        if (CollUtil.isNotEmpty(list)) {
            this.totalCount = list.size();
            this.pageSize = pageSize;
            this.currPage = currPage;
            this.totalPage = (int) Math.ceil((double) totalCount / pageSize);

            if (this.currPage < 1) {
                this.currPage = 1;
            }
            int size = list.size();
            int pageCount = size / pageSize;
            int fromIndex = (this.currPage - 1) * pageSize;
            int toIndex = fromIndex + pageSize;
            if (toIndex >= size) {
                toIndex = size;
            }
            if (this.currPage > pageCount + 1) {
                fromIndex = 0;
                toIndex = 0;
            }
            this.list = list.subList(fromIndex, toIndex);
        } else {
            this.totalCount = 0;
            this.pageSize = 0;
            this.currPage = 0;
            this.totalPage = 0;
            this.list = new ArrayList<>();
        }
    }

    /**
     * Mybatis-Plus 自带分页
     */
    public PageUtils(IPage<?> page) {
        this.list = page.getRecords();
        this.totalCount = Math.toIntExact(page.getTotal());
        this.pageSize = Math.toIntExact(page.getSize());
        this.currPage = Math.toIntExact(page.getCurrent());
        this.totalPage = Math.toIntExact(page.getPages());
    }

    /**
     * pageHelper分页
     */
    public PageUtils(List<?> list) {
        PageInfo<?> pageInfo = new PageInfo<>(list);
        this.list = pageInfo.getList();
        this.totalCount = Math.toIntExact(pageInfo.getTotal());
        this.pageSize = pageInfo.getPageSize();
        this.currPage = pageInfo.getPageNum();
        this.totalPage = pageInfo.getPages();
    }


}
